<template>
    <div ref="patternLock" class="patternLock"></div>
</template>

<script>
import PatternLock from '@/packages/layout/lock/PatternLock.js'
import { mapState } from 'vuex'
export default {
    props: {
        unlock: {
            type: Boolean,
            default: false,
            validator(bool) {
                if (bool && typeof bool !== 'boolean') {
                    console.error('手势解锁是布尔类型')
                }
                return true
            }
        }
    },
    data() {
        return {
            lock: null
        }
    },
    computed: {
        ...mapState({
            lockNum: state => state.lockNum
        })
    },
    mounted() {
        this.lock = new PatternLock(this.$refs.patternLock, {
            enableSetPattern: true,
            /**
             * 当图案绘制完成将图案作为参数传递时将被触发
             * */
            onDraw: pwd => {
                !this.unlock && alert(pwd)
                this.$store.commit('handleLock', pwd)
            }
        })
        this.unlock && this.onCheckPattern()
    },
    methods: {
        onCheckPattern() {
            this.lock.checkForPattern(
                this.lockNum,
                () => {
                    this.$emit('_emit', false)
                    console.log('You unlocked your app')
                    alert('解锁成功')
                },
                () => {
                    this.$emit('_emit', true)
                    console.log('Pattern is not correct')
                }
            )
        }
    }
}
</script>

<style lang="scss" scoped>
@import '../../packages/layout/lock/lock';
.patternLock {
    width: 310px;
    height: 310px;
    margin: 0 auto;
    border-radius: 14px;
    background: hsla(0%, 0%, 100%, 0.1);
}
</style>
